<template>
	<view class="container">
		<view class="login-box">
			<view class="title">账号登录</view>
			<view class="input-group">
				<view class="input-item">
					<text class="icon">👤</text>
					<input 
						type="text"
						v-model="username"
						placeholder="请输入账号"
						placeholder-class="placeholder"
					/>
				</view>
				<view class="input-item">
					<text class="icon">🔒</text>
					<input 
						type="password"
						v-model="password"
						placeholder="请输入密码"
						placeholder-class="placeholder"
					/>
				</view>
			</view>
			<view class="privacy-check">
				<view class="checkbox-wrapper" @click="togglePrivacyCheck">
					<text class="checkbox" :class="{ 'checked': isPrivacyChecked }">{{ isPrivacyChecked ? '✓' : '' }}</text>
					<text class="privacy-text">我已阅读并同意</text>
					<text class="privacy-link" @click.stop="goToPrivacy">《隐私政策》</text>
				</view>
			</view>
			<button class="login-btn" @click="handleLogin">登 录</button>
		</view>
	</view>
</template>

<script>
import { mapActions } from 'vuex'

export default {
	data() {
		return {
			username: 'admin',
			password: 'root',
			isPrivacyChecked: false
		}
	},
	onLoad() {
		// 检查是否已经登录
		this.checkLoginStatus()
	},
	methods: {
		...mapActions(['login', 'checkLoginStatus']),
		togglePrivacyCheck() {
			this.isPrivacyChecked = !this.isPrivacyChecked
		},
		goToPrivacy() {
			uni.navigateTo({
				url: '/pages/privacy/privacy'
			})
		},
		async handleLogin() {
			if (!this.isPrivacyChecked) {
				uni.showToast({
					title: '请先同意隐私政策',
					icon: 'none'
				})
				return
			}
			
			if (!this.username || !this.password) {
				uni.showToast({
					title: '请输入用户名和密码',
					icon: 'none'
				})
				return
			}

			try {
				// 调用登录action
				const userInfo = await this.login({
					username: this.username,
					nickname: this.username,
					avatar: '/static/default-avatar.png'
				})

				uni.showToast({
					title: '登录成功',
					icon: 'success',
					duration: 1500,
					success: () => {
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/account/account'
							})
						}, 1500)
					}
				})
			} catch (error) {
				uni.showToast({
					title: '登录失败',
					icon: 'none'
				})
			}
		},
		async checkLoginStatus() {
			const isLoggedIn = await this.checkLoginStatus()
			if (isLoggedIn) {
				uni.switchTab({
					url: '/pages/account/account'
				})
			}
		}
	}
}
</script>

<style>
.container {
	min-height: 100vh;
	background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 40rpx;
}

.login-box {
	width: 100%;
	background: rgba(255, 255, 255, 0.9);
	border-radius: 30rpx;
	padding: 60rpx 40rpx;
	backdrop-filter: blur(10px);
}

.title {
	font-size: 48rpx;
	color: #333;
	text-align: center;
	margin-bottom: 60rpx;
	font-weight: bold;
}

.input-group {
	margin-bottom: 60rpx;
}

.input-item {
	background: #f8f8f8;
	border-radius: 16rpx;
	height: 100rpx;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
	margin-bottom: 30rpx;
}

.icon {
	font-size: 40rpx;
	margin-right: 20rpx;
}

.input-item input {
	flex: 1;
	height: 100%;
	font-size: 32rpx;
}

.placeholder {
	color: #999;
}

.login-btn {
	background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
	height: 100rpx;
	line-height: 100rpx;
	border-radius: 50rpx;
	color: #fff;
	font-size: 36rpx;
	border: none;
}

.login-btn::after {
	border: none;
}

.privacy-check {
	margin-bottom: 40rpx;
}

.checkbox-wrapper {
	display: flex;
	align-items: center;
	justify-content: center;
}

.checkbox {
	width: 36rpx;
	height: 36rpx;
	border: 2rpx solid #6366F1;
	border-radius: 8rpx;
	margin-right: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	font-size: 24rpx;
}

.checkbox.checked {
	background: #6366F1;
}

.privacy-text {
	font-size: 26rpx;
	color: #666;
}

.privacy-link {
	font-size: 26rpx;
	color: #6366F1;
}
</style>